package com.lala.commom.util;

import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

public class AliyunOssUntil {

    public static final String endpoint = "oss-cn-beijing.aliyuncs.com";
    public static final String accessKeyId = "LTAI5tQL4n7pdN6MhqsFeeYo";
    public static final String accessKeySecret = "jdRBrr8ZySzLluYVDMomHkZeiEycWO";
    public static final String bucketName = "la7";


    public static URL chuantu(String filename, InputStream inputStream){
      OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, filename, inputStream);
        ossClient.shutdown();
        // 获取图片的链接
        GeneratePresignedUrlRequest urlRequest =
                new GeneratePresignedUrlRequest(bucketName, filename, HttpMethod.GET);
        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
        urlRequest.setExpiration(expiration);
        // 获取图片链接
        URL url = ossClient.generatePresignedUrl(urlRequest);
        System.out.println("上传图片完成！");
        return url;
    }



}
